package org.ytor.core.sqlflow.dsl.dsqlfile;

import lombok.Data;
import org.ytor.core.sqlflow.dsl.eval.MapEvalContext;
import org.ytor.core.sqlflow.dsl.template.RenderResult;
import org.ytor.core.sqlflow.dsl.template.TemplateRenderer;
import org.ytor.core.sqlflow.dsl.template.node.support.BlockNode;
import org.ytor.core.sqlflow.enums.SqlType;

import java.util.Map;

/**
 * created by yangtong on 2025/8/7 17:39:54
 * <br/>
 * dsql文件段落
 */
@Data
public class DsqlSegment {
    /**
     * 段落id
     */
    private String id;

    /**
     * 段落所属文件
     */
    private String source;
    /**
     * 段落描述
     */
    private String desc;
    /**
     * 类型
     */
    private SqlType type;

    /**
     * DSL模板
     */
    private String template;

    /**
     * 根据模板解析出的AST结构数据
     */
    private BlockNode root;

    /**
     * 根据参数解析DSL模板
     */
    public RenderResult parse(Map<String, Object> params) {
        MapEvalContext ctx = new MapEvalContext()
                .putAll(params);

        TemplateRenderer renderer = new TemplateRenderer();
        return renderer.render(root, ctx);
    }
}

